package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeleteDishException;
import com.sky.exception.DeleteException;
import com.sky.exception.InsertException;
import com.sky.exception.UpdateException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.utils.RedisDeleteKeysUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    //缓存清理工具
    @Autowired
    private RedisDeleteKeysUtil redisDeleteKeysUtil;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSetMeal(SetmealDTO setmealDTO) {
        //先处理套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        int insertedResult = setMealMapper.insert(setmeal);
        if (insertedResult == StatusConstant.RETURN0) {
            throw new InsertException(MessageConstant.INSERT_FAILED);
        }
        //处理套餐菜品表
        Long insertSetMealID = setmeal.getId();
        if (setmealDTO.getSetmealDishes() != null && !setmealDTO.getSetmealDishes().isEmpty()) {
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(insertSetMealID);
            });
            setMealDishMapper.insertSetMealDish(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> pageResult = setMealMapper.queryPage(setmealPageQueryDTO);
        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }

    /**
     * 根据套餐id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetMealById(Long id) {
        //先查询套餐基本数据
        SetmealVO setMealByIdResult = setMealMapper.getSetMealById(id);
        //再查询套餐菜品数据
        List<SetmealDish> setMealDishList = setMealDishMapper.getSetMealDishList(id);
        setMealByIdResult.setSetmealDishes(setMealDishList);
        return setMealByIdResult;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSetMeal(SetmealDTO setmealDTO) {
        //先处理套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        int updateResult = setMealMapper.updateSetMeal(setmeal);
        if (updateResult == StatusConstant.RETURN0) {
            throw new UpdateException(MessageConstant.UPDATE_FAILED);
        }
        //处理套餐菜品表
        //先删后插入
        List<Long> setMealIds = new ArrayList<>();
        setMealIds.add(setmealDTO.getId());
        int result = setMealDishMapper.deleteSetMealDishBySetMealIds(setMealIds);
        if (result == StatusConstant.RETURN0) {
            throw new DeleteException(MessageConstant.FAIL_TO_DELETE);
        }
        //处理套餐菜品表
        if (setmealDTO.getSetmealDishes() != null && !setmealDTO.getSetmealDishes().isEmpty()) {
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setMealDishMapper.insertSetMealDish(setmealDishes);
        }
    }
    /**
     * 套餐起售停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public void EditSetMealStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        int updateResult = setMealMapper.updateSetMeal(setmeal);
        if (updateResult == StatusConstant.RETURN0) {
            throw new UpdateException(MessageConstant.UPDATE_FAILED);
        }
        //缓存清理
        //根据套餐的主键(id)查询他们的菜品分类id
        List<Long> setMealList = setMealMapper.getCategoryIdBySetMealId(new ArrayList<Long>() {{add(id);}});
        //根据菜品分类id删除缓存
        redisDeleteKeysUtil.deleteSetMealKeys(setMealList);
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSetMeal(List<Long> ids) {
        //先删除套餐菜品表
        int deleteSetMealDishResult = setMealDishMapper.deleteSetMealDishBySetMealIds(ids);
        if (deleteSetMealDishResult == StatusConstant.RETURN0) {
            throw new DeleteDishException(MessageConstant.FAIL_TO_DELETE);
        }
        //再删除套餐表
        int deleteSetMealResult = setMealMapper.deleteSetMeal(ids);
        if (deleteSetMealResult == StatusConstant.RETURN0) {
            throw new DeleteException(MessageConstant.FAIL_TO_DELETE);
        }
        //根据套餐的主键(id)查询他们的菜品分类id
        //根据菜品分类id删除缓存
        redisDeleteKeysUtil.deleteSetMealKeys(setMealMapper.getCategoryIdBySetMealId(ids));
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
